package keter.web.controller.base;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;

import keter.framework.web.controller.AbstractBaseController;
import keter.model.User;
import keter.security.AnonymousUserAdapter;
import keter.security.UserAdapter;

@Controller
public class KeterBaseController extends AbstractBaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(KeterBaseController.class);

    public KeterBaseController() {

    }

    /**
     * <p>
     * Method ：populateCurrentUser
     * <p>
     * Description : 将当前用户放入JSTL全局变量
     * 
     * @return
     * @author gulixing@msn.com
     * @version 1.0.0
     */
    @ModelAttribute("user")
    public User populateCurrentUser() {
        return getCurrentUser();
    }

    protected  User getCurrentUser() {        
        return getCurrentUserAdapter().getUser();
    }
    
    protected  User getCurrentUser(boolean fromCache) {        
        return getCurrentUserAdapter().getUser();
    }

    protected  boolean isLoggedIn() {
        return !(getCurrentUserAdapter() instanceof AnonymousUserAdapter);
    }
    
    private  UserAdapter getCurrentUserAdapter() {
        Object principal =  getCurrentUserPrincipal();
        if (principal instanceof UserAdapter) {
          return ((UserAdapter) principal);
      }
        return new AnonymousUserAdapter();
    }
}